﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class SupervisorFullView : Page
    {
        private static int _staffId;
        private static string _type;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (int.TryParse(Request.QueryString["staffId"], out _staffId))
                {
                    UpdatePanel1.Visible = Roles.GetRolesForUser(User.Identity.Name).Contains("Manager");

                    LoadDetailsView(_staffId);
                    if (_type == "Vehicles supervisor")
                    {
                        LoadGridview();
                    }
                    else
                    {
                        UpdatePanel1.Visible = false;
                    }
                }
                else
                {
                    Response.Redirect("Supervisors.aspx");
                }
            }
        }

        private void LoadDetailsView(int staffId)
        {
            var staff = StaffBLL.GetStaffById(staffId);
            var supervisor = new Supervisor(staff)
            {
                SupervisorType = SupervisorBLL.GetSupervisorTypeById(staffId)
            };
            _type = supervisor.SupervisorType;
            DetailsView1.DataSource = new List<Supervisor> {supervisor};
            DetailsView1.DataBind();
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            Response.Redirect("EditSupervisorForm.aspx?staffId=" + Request.QueryString["staffId"]);
        }

        private void LoadGridview()
        {
            gvVehicles.DataSource =
                VehicleBLL.GetAllVehicles()
                    .Where(v => v.SupervisorId == _staffId)
                    .OrderByDescending(v => v.RegistrationNo)
                    .ToList();
            gvVehicles.DataBind();
        }

        protected void gvVehicles_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvVehicles.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            var keyword = tbSearch.Text.Trim();
            if (string.IsNullOrEmpty(keyword))
            {
                LoadGridview();
            }
            else
            {
                var vehicle = VehicleBLL.GetVehiclesByRegNo(keyword);

                gvVehicles.DataSource = vehicle == null ? null : new List<Vehicle> {vehicle};
                gvVehicles.DataBind();
            }
        }

        protected void ibDelete_OnClick(object sender, ImageClickEventArgs e)
        {
            var regNo = Request.QueryString["RegNo"];
            VehicleBLL.RemoveVehicle(regNo);
            LoadGridview();
        }
    }
}